﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using System.Threading;
using System.Text;
using System.Web;

using ESF.General;
using System.Web.UI;
using System.IO.Compression;
using System.IO;
using System.Net;

#endregion


namespace ESF.Web
{
	public static class RequestHelper
	{
		#region Propiedades
		
		public static string ApplicationPath
		{
			get 
			{
				string path;
				
				
				path = HttpContext.Current.Request.ApplicationPath;
				
				if (path != "/")
				{
					path += "/";
				}
				
				return path;
			}
		}
		
		#endregion
		
	
		#region Funciones lectura de parametros post de control
		
		public static string GetPostParam (Control pControl)
		{
			return HttpContext.Current.Request.Form [pControl.UniqueID];
		}
		
		
		public static int? GetPostParamInt (Control pControl)
		{
			return TextHelper.GetNullableInt (GetPostParam (pControl));
		}
		
		
		public static byte GetPostParamByte (Control pControl)
		{
			return TextHelper.GetByte (GetPostParam (pControl));
		}
		
		
		public static bool GetPostParamBool (Control pControl)
		{
			return TextHelper.GetBool (GetPostParam (pControl));
		}
		
		
		public static long GetPostParamLong (Control pControl)
		{
			return TextHelper.GetLong (GetPostParam (pControl));
		}
		
		#endregion

	
		#region Funciones Lectura de parametros post
	
		public static string GetPostParam (string pName)
		{
			return HttpContext.Current.Request.Form [pName];
		}
		
		
		public static int? GetPostParamInt (string pName)
		{
			return TextHelper.GetNullableInt (GetPostParam (pName));
		}
		
		
		public static byte? GetPostParamByte (string pName)
		{
			return TextHelper.GetNullableByte (GetPostParam (pName));
		}
		
		
		public static bool? GetPostParamBool (string pName)
		{
			return TextHelper.GetNullableBool (GetPostParam (pName));
		}
		
		
		public static long? GetPostParamLong (string pName)
		{
			return TextHelper.GetNullableLong (GetPostParam (pName));
		}


		public static double? GetPostParamDouble (string pName)
		{
			return TextHelper.GetNullableDouble (GetPostParam (pName));
		}
		
		#endregion
		
		
		#region Lectura de GET
		
		public static string GetQueryStringValue (string pQueryString, string pVarName)
		{
			string [] queryStringParts;
			
			
			if (TextHelper.IsEmpty (pQueryString)) { return null; }
			
			if (pQueryString.Contains ("?"))
			{
				pQueryString = pQueryString.Substring (pQueryString.IndexOf ('?') + 1);
			}
			
			pQueryString = HttpUtility.UrlDecode (pQueryString);
			
			queryStringParts = pQueryString.Split ('&');
			
			
			for (int i = 0; i < queryStringParts.Length; i++)
			{
				if (queryStringParts [i].StartsWith (pVarName))
				{
					return queryStringParts [i].Substring (queryStringParts [i].IndexOf ('=') + 1);
				}
			}
			
			return null;
		}
		
		#endregion
		
		
		#region Ajustes de Cadenas
		
		public static string GetHostWithApplication (HttpRequest pRequest)
		{
			return string.Format ("{0}{1}/", GetHost (pRequest), pRequest.ApplicationPath);
		}


		public static string GetHost (string pURLHost)
		{
			if (string.IsNullOrWhiteSpace (pURLHost))
			{	
				return string.Empty;
			}

			if (pURLHost.Split ('.').Length > 2)
			{
				return pURLHost.Substring (pURLHost.IndexOf ('.'));
			}

			return pURLHost;
		}


		public static string GetHost (HttpRequest pRequest)
		{
			return string.Format ("{0}://{1}:{2}", pRequest.Url.Scheme, pRequest.Url.Host, pRequest.Url.Port);
		}

		
		// TODO: Verificar que si ya esta la variable se reemplaze
		public static string AppendVarValueToHTTPURL (string pUnEncodedURL, params string [] pVarNameVarValue)
		{
			if (TextHelper.IsEmpty (pUnEncodedURL)) { return string.Empty; }

            if (pVarNameVarValue == null || pVarNameVarValue.Length < 2) { return pUnEncodedURL; }
			
			if (pUnEncodedURL.StartsWith ("javascript", StringComparison.InvariantCultureIgnoreCase))
			{
				return pUnEncodedURL;
			}


			if (!pUnEncodedURL.Contains ("?"))
			{
				pUnEncodedURL += "?D=1";
			}
			
			for (int i = 0; i < pVarNameVarValue.Length; i+=2)
			{
				pUnEncodedURL += string.Format ("&{0}={1}", pVarNameVarValue [i], HttpUtility.UrlEncode (pVarNameVarValue [i + 1]));
			}
			
			return pUnEncodedURL;
		}
		
		#endregion
	}
}
